#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>


//void rotate(int* nums, int numssize, int k)
//{
//	k %= numssize;
//	for (int i = 0; i < k; i++)
//	{
//		int tmp = nums[numssize - 1];
//		for (int j = numssize - 1; j > 0; j--)
//		{
//			nums[j] = nums[j - 1];
//		}
//		nums[0] = tmp;
//	}
//}

void reverse(int* a, int left, int right)
{
	while (left < right)
	{
		int tmp = a[left];
		a[left] = a[right];
		a[right] = tmp;
		left++;
		right--;
	}
}

//void rotate(int* nums, int numsSize, int k)
//{
//	k %= numsSize;
//	reverse(nums, 0, numsSize - k - 1);
//	reverse(nums, numsSize - k, numsSize - 1);
//	reverse(nums, 0, numsSize - 1);
//}

void rotate(int* nums, int numsSize, int k)
{
	k %= numsSize;
	int tmp[numsSize];
	int j = k;
	for (int i = 0; i < numsSize - k - 1; i++)
	{
		tmp[j++] = nums[i];
	}

	j = 0;
	for (size_t i = numsSize - k - 1; i < numsSize - 1; i++)
	{
		tmp[j] = nums[i];
	}
	for (size_t i = 0; i < numsSize; i++)
	{
		nums[i] = tmp[i];
	}
}

int main()
{
	int a[5] = { 0,1,2,3,4 };
	int n = sizeof(a) / sizeof(a[0]);
	rotate(a, n, 2);
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
	return 0;
}